package com.geekchique.runningbuddy.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.Menu.Item;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.util.Log;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.controller.IOptionsMenu;
import com.geekchique.runningbuddy.controller.IStopClockController;
import com.geekchique.runningbuddy.controller.OptionsMenuImpl;
import com.geekchique.runningbuddy.controller.StopwatchControllerImpl;
import com.geekchique.runningbuddy.provider.ActivityID;
import com.geekchique.runningbuddy.service.RemoteStopClockServiceInterface;

public class Stopwatch extends Activity {

	private IStopClockController stopClockController;
	private IOptionsMenu optionsMenu;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
        setContentView(R.layout.stopwatch);
		optionsMenu = new OptionsMenuImpl(this);

        stopClockController = new StopwatchControllerImpl(this);
		stopClockController.createStopClockView(this, (TextSwitcher) findViewById(R.id.stopwatch_time), (Button) findViewById(R.id.stopclock_buttonstart) );
		stopClockController.createwatchClockTargetView(
				this,
				(CheckBox) findViewById(R.id.checkbox_target),
				(TextView) findViewById(R.id.hours),
				(TextView) findViewById(R.id.minutes),
				(TextView) findViewById(R.id.seconds),
				(TextView) findViewById(R.id.milliseconds),
				(Button) findViewById(R.id.hours_up),
				(Button) findViewById(R.id.hours_down),
				(TextView) findViewById(R.id.hours_sep),
				(Button) findViewById(R.id.minutes_up),
				(Button) findViewById(R.id.minutes_down),
				(TextView) findViewById(R.id.minutes_sep),
				(Button) findViewById(R.id.secs_up),
				(Button) findViewById(R.id.secs_down),
				(TextView) findViewById(R.id.seconds_sep),
				(Button) findViewById(R.id.millisecs_up),
				(Button) findViewById(R.id.millisecs_down)
		);;
	}


	public Context getContext() {
		return this;
	}

    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		optionsMenu.addAllActivitiesToMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(Item item) {
		optionsMenu.selectItem(item);
	    return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onStop() {
		super.onStop();
		RemoteStopClockServiceInterface.getInstance().activityStopped(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		RemoteStopClockServiceInterface.getInstance().activityStarted(this);
	}

	@Override
	protected void onResume() {
		RemoteStopClockServiceInterface.getInstance().setUpdateHandler(stopClockController.getUpdateHandler());
		stopClockController.onResume();
		super.onResume();
	}

	@Override
	protected void onPause() {
		RemoteStopClockServiceInterface.getInstance().clearUpdateHandler(stopClockController.getUpdateHandler());
		super.onPause();
	}

	public static void show(Activity activity)
	{
		//Only start new activity if we're currently on a different activity
		if (activity.getLocalClassName().compareTo("activity.Stopwatch") != 0)
		{
			Intent intent = new Intent(activity, TabsAdapter.class);
			Bundle extras = new Bundle();
			extras.putString("name0", "Stopwatch");
			extras.putString("tab0", "Stopwatch");
			extras.putString("name1", "Location");
			extras.putString("tab1", "CurrentRunMap");
			extras.putInt("default", 0);
			intent.putExtras(extras);
			activity.startSubActivity(intent, ActivityID.STOPCLOCK);
		}
	}
}
